package seleniumdemo.common.driver;



import lombok.Data;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;


/****
 *
 */
@Data
public class DriverBase {
    //web驱动
    private WebDriver driver;

    //JavascriptExecutor javaScript =(JavascriptExecutor)driverBase.getDriver();

    //当前url
    private String url;

    //构造函数
    public DriverBase(String brower, String _url) {
        SelectDriver selectDriver = new SelectDriver();
        this.driver = selectDriver.getDriver(brower);
        driver.get(_url);
        this.url = _url;
    }

    //构造函数
    public DriverBase(String brower) {
        SelectDriver selectDriver = new SelectDriver();
        this.driver = selectDriver.getDriver(brower);
    }

    //findElement的封装
    public WebElement findElement(By by) {
        WebElement webElement = driver.findElement(by);
        return webElement;
    }

    //findElements的封装
    public List<WebElement> findElements(By by) {
        List<WebElement> webElements = driver.findElements(by);
        return webElements;
    }

    //打开一个网页
    public void openUrl(String _url) {
        driver.get(_url);
        this.url = _url;
    }

    //打开一个新的标签页
    public void operNewWindows(String url){
        String js="window.open('"+url+"')";
        ((JavascriptExecutor)this.driver).executeScript(js);

    }

    //修改url
    public void setUrl(String _url) {
        openUrl(_url);
    }

    //窗口最大化
    public void windowsMax() {
        driver.manage().window().maximize();
    }

    //关闭当前页
    public void close() {
        driver.close();
    }

    //关闭浏览器
    public void quit() {
        driver.quit();
    }

    //获取当前标签页
    public String getTitle(){
        return  driver.getTitle();
    }
}
